package indi.latch;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Title: SemaphoreDemo
 * Description:
 * Copyright: 数禾科技 Copyright(c) 2021/6/3
 * Encoding: UNIX UTF-8
 *
 * @author 徐林
 */
public class SemaphoreDemo {

    private static final Logger LOGGER = Logger.getLogger(SemaphoreDemo.class.getName());

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(2);
        try {
            if (semaphore.tryAcquire(3, 1, TimeUnit.MINUTES)) {
                System.out.println("acquire 3 permites successful");
            }
            System.out.println("acquire 3 permites timeout");
        } catch (InterruptedException e) {
            System.out.println("acquire 3 permites exceptional");
            LOGGER.log(Level.SEVERE, "信号量超时");
        }
    }
}
